<template>
<div id="Weather" class="animate__animated animate__bounceInDown">
  <main>
<!--    <div class="search-box">-->
<!--      <input type="text"-->
<!--             class="search-bar"-->
<!--             placeholder="请输入要查询的城市"-->
<!--             v-model="query"-->
<!--             @keyup.enter="getWeather"-->
<!--      />-->
<!--    </div>-->
    <div class="show">
      <div class="weather-wrap" >
<!--        <div class="location-box" v-show="isShowWeather">-->
<!--          <div class="location">{{location}} · {{weather}}</div>-->
<!--           <div class="date">{{date}}</div>-->
<!--        </div>-->
        <span class="img"><img v-bind:src=weatherImg alt=""></span>
        <div class="weather-box" v-show="isShowWeather">
          <div class="temp">{{temp}}℃</div>
<!--          <div class="weather">{{weather}}</div>-->
        </div>
      </div>
    </div>

  </main>
</div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'Weather',
  data() {
    return{
      api_key: 'bd1a6d8c0c0d4ebe9744fd4bd61e36b2',
      base_url: 'https://free-api.heweather.net/s6/weather/now',
      query: '',
      weatherImg: 'src/assets/weather_icon/clouds.png',
      location: '',
      temp:'',
      weather:'',
      isShowWeather: false,
    }
  },
  computed: {
    date() {
      const date = new Date()
      date.getFullYear
      date.getMonth + 1
      date.getDate
      return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    },
  },
  methods:{
    getWeather() {
      axios
          .get(`${this.base_url}?location=${this.query}&key=${this.api_key}`)
          .then(res => {
            //console.log(res)
            res = res.data.HeWeather6[0]
            this.location = res.basic.location
            this.temp = res.now.tmp
            this.weather = res.now.cond_txt
            this.isShowWeather = true
            let weather_arr = ['雨','阴','多云','晴','雪','雾']
            for(let i in weather_arr){
              // console.log(this.weather)
              let regex = new RegExp(weather_arr[i])
              let _weather = regex.test(this.weather)
              // console.log(_weather)
              if(_weather) {
                this.weatherImg = "src/assets/weather_icon/"+i+'.png';
              }
            }
          })

    },
    getname(){
      let name = returnCitySN.cname
      let i = 0;
      for(;i < name.length; i++)
        if(name[i] === '省'){
          name = name.slice(i+1,)
        }else if(name[i] === '地'&& name[i+1] === '区'){
          name = name.slice(0,i)
        }
      if(name.type === undefined)
        this.query = '广州';
      else
        this.query = name
      // console.log(name)
    }

  },
  mounted() {
    this.getname()
    this.getWeather()
  }
}
</script>

<style scoped lang="less">
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
#Weather{
  background-image: url("../assets/weather_icon/clouds.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position:  -3rem 5rem ;
  transition: 0.4s;
  padding: 12px 20px;
  background-color: #b2d9d6;
  border-radius: 25px;
  box-shadow: 2px 5px 10px 1px rgba(0 0 0 / 20%);
}
main{
  height: 240px;
  width: 240px;
}
.search-box{
  width: 60%;
  //margin-bottom: 10px;
  .search-bar{
    display: block;
    width: 222px;
    padding: 15px;
    color: #313131;
    font-size: 20px;
    box-shadow: 0px 0px 8px rgba(255,255,255,0.7);
    background-color: rgba(255,255,255,0.7);
    border-radius: 0px 16px;
    transition: 0.4s;
    &:focus{
      background-color: rgba(255,255,255, 0.9);
      border-radius: 5px;
    }
  }
}

.img{
  img{
    position: absolute;
    left: 12px;
    top: 22px;
  }
}
.show{
  position: relative;
  background-color: #ffffff;
  box-sizing: border-box;
}
.location-box{

  position: absolute;
  box-sizing: border-box;
    display: block;
    color: #3d3b4f;
  .location{
    font-size: 32px;
    font-weight: 800;
    text-align: center;
    text-shadow: 1px 1px rgba(0,0,0,0.60);
  }
  .date{
    font-size: 30px;
    font-weight: 100;
    text-align: center;
    margin-top: 16px;
  }
}
.weather-box{
  position: absolute;
  text-align: center;
  left: 240px;
  color: #3d3b4f;
  text-shadow: 1px 1px rgba(0,0,0,0.60);
  .temp{
    font-family: SimHei;
    color: #fff8e6;
    position: absolute;
    right: 0px;
    top: 20px;
    font-size: 60px;
    font-weight: 550;
    margin: 0 0 10px 0;
  }
  .weather{
    font-size: 24px;
    font-weight: 700;
  }
}
</style>